<template>
  <div>
    <div
      v-for="(route,index) in moduleList"
      v-if="!route.hidden"
      :key="index"
      class="icon-list">
      <h5 v-show="showTitle(route)">{{ route.meta.title }}</h5>
      <ul class="icon-list">
        <router-link
          v-for="child in route.children"
          v-if="!child.hidden"
          :to="{path:resolvePath(route.path,child.path),query:resolveParams(child.params)}"
          :key="child.path">
          <li>
            <span>
              <i><svg-icon :icon-class="child.meta.icon" :style="{ color: child.color}"/></i>
              <span class="icon-name">{{ child.meta.title }}</span>
            </span>
          </li>
        </router-link>
      </ul>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import Config from '@/config'
  import path from 'path'

  export default {
    data() {
      return {
        module: Config.module,
        root: Config.root
      }
    },
    computed: {
      ...mapGetters([
        'permission_routers'
      ]),
      moduleList() {
        const rootModule = this.permission_routers.filter(route => route.name === this.root)
        const rootChildren = rootModule[0].children
        return rootChildren.filter(route => route.module === this.module.module)
      }
    },
    mounted() {
      document.getElementsByClassName('app-main')[0].scrollTop = this.$store.state.currentScroll
    },
    methods: {
      resolvePath(basepath, routePath) {
        return path.resolve(basepath, routePath)
      },
      resolveParams(params) {
        try {
          if (params) {
            return JSON.parse(params)
          }
        } catch (err) {
          return null
        }
        return null
      },
      showTitle(route) {
        let flag = false
        if(route.children){
          route.children.forEach(item => {
            if (!item.hidden) {
              flag = true
            }
          })
        }
        return flag
      }
    },
    destroyed() {
      this.$store.state.currentScroll = document.getElementsByClassName('app-main')[0].scrollTop
    },
  }
</script>

<style scoped>
  .icon-list {
    overflow: hidden;
    list-style: none;
    padding: 0 !important;
    border-radius: 4px;

  }

  h5 {
    color: #333333;
    font-size: 16px;
    font-weight: normal;
    margin-bottom: 8px;
    padding-left: 10px;
    border-left: 3px solid #1955AF;
  }

  .icon-list li {
    float: left;
    width: 13.68%;
    text-align: center;
    height: 130px;
    cursor: pointer;
    line-height: 130px;
    color: #666;
    font-size: 13px;
    border: 1px solid #e7e7e7;
    border-radius: 4px;
    margin-right: 10px;
    margin-bottom: 10px;
  }

  .icon-list li:hover {
    box-shadow: 2px 5px 10px rgba(0, 0, 0, 0.2);

  }

  .icon-list li span {
    line-height: normal;
    transition: color .15s linear;
    display: inline-block;
    vertical-align: middle;
  }

  .icon-list li .icon-name {
    display: inline-block;
    padding: 5px 3px;
    height: 3em;
	font-weight: 700;
  }

  .icon-list li i {
    display: block;
    font-size: 32px;
    margin-bottom: 15px;
    line-height: 20px;
    color: #ffffff;
    transition: color .15s linear;
  }

  .svg-icon {
    font-size: 50px;
    color: #1296db;
  }
</style>
